package com.dq.controller.app.account;

import com.dq.controller.in.AccountQuery;
import com.dq.domain.sys.user.Account;
import com.dq.domain.sys.user.center.UserAccountStaffUser;
import com.dq.exception.customException.AdminException;
import com.dq.service.AllService;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2020/4/13 16:35
 */
@RestController
@RequestMapping("/app/account")
@Api(description = "充值记录")
public class AccountController extends AllService {
    @PostMapping("list")
    @ApiOperation("充值记录列表")
    public Vo list (@RequestBody AccountQuery accountQuery) throws AdminException{
        List<Account> list = accountService.list(accountQuery);
        List<Account.FrontAccount> toList = new ArrayList<>();
        for (Account account : list) {
            Account.FrontAccount  frontAccount= account.toFrontAccount();
            UserAccountStaffUser userAccountStaffUser = userAccountStaffUserRepository.findByAccountAndStaffUserAndDeletedFalse(account,account.getStaffUser());
            List<UserAccountStaffUser> userAccountStaffUsers = userAccountStaffUserRepository.findByAccountAndDeletedFalse(account);
            if(userAccountStaffUser != null && userAccountStaffUsers != null && userAccountStaffUsers.size() > 0) {
                String waiterPeople = "";
                for(UserAccountStaffUser vo: userAccountStaffUsers){
                    waiterPeople = waiterPeople+" "+vo.getStaffUser().getUserRealcName();
                }
                frontAccount.setWaiterNum((long) userAccountStaffUsers.size());
                frontAccount.setWaiterPeople(waiterPeople);
                frontAccount.setMyAchievements(userAccountStaffUser.getAchievements());
            }
            toList.add(frontAccount);
        }
        return new ListDataVo(toList,accountService.accountCount(accountQuery));
    }
}
